﻿Imports System.IO
Imports System.Text

Module Module1

	Sub Main()
		Console.WriteLine("Parses a part of an rnc file that contain enums and converts them to vb.net.")

		Console.WriteLine("File input: {0}", System.Environment.GetCommandLineArgs(1))

		Dim RncFileDirectory, RncFilePath As String

		RncFileDirectory = Path.GetDirectoryName(System.Environment.GetCommandLineArgs(1))
		RncFilePath = System.Environment.GetCommandLineArgs(1)

		Dim result As New StringBuilder()
		Dim reader As New StreamReader(RncFilePath)
		Dim line As String = reader.ReadLine()
		Dim NumberOfEnums As Integer

		Do While Not reader.EndOfStream
			'Check to see if this line defines a new enum
			If line.Contains("_EnumValue = ") Then
				Dim values() As String

				values = line.Substring(line.IndexOf("="c)).Split("|"c)

				result.AppendLine("Public Enum " & line.Substring(0, line.IndexOf("_")))
				For Each value As String In values
					Dim enumvalue As String

					enumvalue = value.Substring(value.IndexOf("""") + 1, value.LastIndexOf("""") - value.IndexOf("""") - 1)

					'VB language reserved keywords should be surrounded with [ ]
					If enumvalue = "Default" Or
					 enumvalue = "Stop" Or
					 enumvalue = "Resume" Or
					 enumvalue = "Nothing" Or
					 enumvalue = "In" Or
					 enumvalue = "Double" Then
						enumvalue = "[" & enumvalue & "]"
					End If

					result.AppendLine(ControlChars.Tab & enumvalue)
				Next
				result.AppendLine("End Enum")
				result.AppendLine()

				NumberOfEnums += 1
			End If

			line = reader.ReadLine()
		Loop

		Console.WriteLine("Enums found:" & NumberOfEnums)
		My.Computer.FileSystem.WriteAllText(Path.Combine(RncFileDirectory, "Enums.vb"), result.ToString(), False)

		Console.ReadLine()
	End Sub

End Module
